import sheep from '@/sheep';
import { formatImageUrlProtocol, getWxaQrcode } from './index';

const user = async (poster) => {
  const width = poster.width;         // 海报总宽度
  const height = poster.height;       // 海报总高度
  // const height = '100%';       // 海报总高度
  const userInfo = sheep.$store('user').userInfo;
  const wxa_qrcode = await getWxaQrcode(poster.shareInfo.path, poster.shareInfo.query);

  // 正方形框尺寸（取总宽度的 0.5）
  const squareSize = width * 0.5;
  // 正方形框左上角坐标，让它水平居中、垂直略上
  const squareLeft = (width - squareSize) / 2;
  const squareTop  = (height - squareSize) / 2 - 20; // 可以根据实际调整 -20px
  // 二维码尺寸：略小于正方形框，内边距留 16px
  const qrSize     = squareSize - 32;
  const qrLeft     = squareLeft + 16;
  const qrTop      = squareTop + 16;
  return [
    // 1. 背景图／背景色
    {
      type: 'image',
      src: formatImageUrlProtocol(
        sheep.$url.cdn(sheep.$store('app').platform.share.posterInfo.user_bg)
      ),
      css: {
        top: 0,
        left: 0,
        width,
        height: '100%',
        position: 'fixed',
        objectFit: 'cover',
        zIndex: -1,
      },
    },

    // 2. 正方形白框（圆角）
    {
      type: 'rect',
      css: {
        top: squareTop,
        left: squareLeft,
        width: squareSize,
        height: squareSize,
        background: '#FFFFFF',
        borderRadius: 20,
      }
    },

    // 3. 二维码 — 放在白框里
    //    小程序端用 getWxaQrcode 拿到的 image，H5 用 qrcode 类型也可
    ...(
      sheep.$platform.name === 'WechatMiniProgram' || sheep.$platform.name === 'MP-WEIXIN'
      ? [{
          type: 'image',
          src: wxa_qrcode,
          css: {
											position: 'fixed',
											top: '200rpx',
											left: qrLeft,
											width: qrSize,
											height: qrSize,
          }
        }]
      : [{
          type: 'qrcode',
          text: poster.shareInfo.link,
          css: {
            position: 'fixed',
            top: 105,
            left: qrLeft,
            width: qrSize,
            height: qrSize,
          }
        }]
    ),

    // 4. 用户昵称 — 左下角，距左 20px，距底 40px
				...(
					sheep.$platform.name === 'WechatMiniProgram' || sheep.$platform.name === 'MP-WEIXIN'
						? [
							{
							  type: 'text',
							  text: userInfo.nickname,
							  css: {
							    position: 'fixed',
							    left: 18,
							    bottom: '80rpx',
							    fontSize: 16,
							    color: '#ffffff',
							    fontWeight: '600',
							  }
							}
						] : [
							{
							  type: 'text',
							  text: userInfo.nickname,
							  css: {
							    position: 'fixed',
							    left: 18,
							    bottom: '100rpx',
							    fontSize: 16,
							    color: '#ffffff',
							    fontWeight: '600',
							  }
							}
						]
				),

    // 5. 用户头像 — 右下角，距右 20px，距底 30px
				...(
					sheep.$platform.name === 'WechatMiniProgram' || sheep.$platform.name === 'MP-WEIXIN'
						? [
							{
							  type: 'image',
							  src: formatImageUrlProtocol(sheep.$url.cdn(userInfo.avatar)),
							  css: {
							    position: 'fixed',
							    width: 55,
							    height: 55,
							    borderRadius: 30,
							    right: 15,
							    bottom: 15,
							  }
							}
						] : [
							{
							  type: 'image',
							  src: formatImageUrlProtocol(sheep.$url.cdn(userInfo.avatar)),
							  css: {
							    position: 'fixed',
							    width: 55,
							    height: 55,
							    borderRadius: 30,
							    right: 15,
							    bottom: 15,
							  }
							}
						]
				)
  ];
};

export default user;
